<?php


namespace app\mobile\controller;
use app\common\JSSDK;

use app\common\wechatmini\wxBizDataCrypt;

use app\common\WechatPay as WechatPayModel;
use app\model\Member as SelfModel;
use app\model\MemberInvite;
use app\model\MemberExpire;
use app\model\MemberPackage;
use app\validate\Member as SelfValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Db;

class Wechat extends Base
{
    /**
     * 微信小程序 登陆/支付和太阳码等;
     */
    /**
     * 验证码
     */
    public function captcha($mobile, $type)
    {

        try {
            if(empty($mobile)){
                return json(['code' => 400, 'message' => '请填写手机号']);
            }
            $find = (new SelfModel())->where('mobile', $mobile)->find();
            if ($type == 'register' && !empty($find)) {
                return json(['code' => 400, 'message' => '用户已注册']);
            }
            if ($type == 'captcha' && !empty($find)) {
                //小程序注册 无需处理 存在不存在都没事
            }
            if (($type == 'update_password' || $type == 'forget_password' || $type == 'set_pay_password') && empty($find)) {
                return json(['code' => 400, 'message' => '用户不存在']);
            }

            if ($type == 'update_username_old' && empty($find)) { //修改手机号的老手机号 必须存在
                return json(['code' => 400, 'message' => '用户不存在']);
            }
            if ($type == 'update_username' && !empty($find)) { //修改手机号的新手机号必须 不存在
                return json(['code' => 400, 'message' => '用户已注册']);
            }

            if($type == 'invite' && !empty($find)){
                return json(['code' => 400, 'message' => '用户已注册']);
            }
            $smsVerify = Cache::get('captcha_' . $type . '_expire_' . $mobile);
            if (!empty($smsVerify)) {
                // return ['code' => 401, 'message' => '60s内只能发送一次'];
            }
            $code = rand(100000, 999999);
            $result = sendSMS($mobile, $code);

            if ($result) {
                Cache::set('captcha_' . $type . '_expire_' . $mobile, $code, 60); // 短信请求有效期
                writeLog('captcha1_' . $type . '_expire_' . $mobile,'web_register_data');
                writeLog('captcha_'.Cache::get('captcha2_' . $type . '_expire_' . $mobile),'web_register_data');
                Cache::set('captcha_' . $type . '_save_' . $mobile, $code, 3600);     // 短信保存有效期
                return json(['code' => 200, 'message' => '发送成功', 'captcha' => $code]);
            }
            return json(['code' => 400, 'message' => '失败']);
        } catch (\Exception $e) {
            return json(['code' => 500, 'message' => $e->getMessage()]);
        }
    }


    /**
     * 生成网站端预支付单openid_session_key
     * member_id
     * type_id
     * price
     * order_number
     * 从订单那里获取价格订单号在生成预支付单
     * 价格不可以使用前提上传 否者价格支付了BUG
     */
    public function unifiedorder($order_number)
    {
            //$order_number = $this->order_number();
        $order = \app\model\Order::where('order_number',$order_number)->find();
        $openid = (new \app\model\Member())->where('id',$this->member_id)->value('openid');
        Db::name('unifiedorder')->insert(['member_id' => $this->member_id,'type_id' => 1,'way_id' => 2,'price' => $order['order_price'],'order_number' => $order_number,'status' => 1,'create_time' => date('Y-m-d H:i:s')]);
        $ret = (new WechatPayModel('mobile', 'JSAPI', $openid,'order'))->unifiedorder($order_number,$order['order_price']*100);

        return view('',['jsApiParameters'=>json_encode($ret),'order_price'=>$order['order_price']]);
    }

    /**
     * 充值预支付单接口
     */
    public function recharge()
    {
        try {
            $price = MemberPackage::where('id', $this->param['package_id'])->value('price');
            $order_number = order_number();
            Db::name('unifiedorder')->insert(['member_id' => $this->param['member_id'], 'type_id' => 2, 'way_id' => 2, 'price' => $price, 'order_number' => $order_number, 'status' => 1, 'create_time' => date('Y-m-d H:i:s')]);
            $ret = (new WechatPayModel('app', 'APP', $this->param['openid'], 'recharge'))->unifiedorder($order_number);
            return json(['code' => 200, 'message' => '预支付单信息', 'data' => $ret]);
        } catch (\Exception $e) {
            return json(['code' =>$e->getCode(),'message' => $e->getMessage(),'line' => $e->getLine()]);
        }

    }

    public function refund(){
        $ret = (new WechatPayModel('mobile', 'JSAPI', '','order'))->refund();
        halt($ret);
    }

    /**
     * 微信支付回调
     */
    public function notifyurl()
    {

        $ret = (new WechatPayModel('mobile'))->notifyurl();
        return true; //ret肯定是true 数据里返回不返回true不重要
    }







}